In [1]:
## 사전 실행 코드
import polars as pl
df_boxoffice = pl.read_csv("D:/book/polars/data/movie/KOBIS_기간별박스오피스.csv", skip_rows = 4, try_parse_dates=True)
boxoffice_cols = df_boxoffice.columns
boxoffice_cols[4] = '매출액점유율'
df_boxoffice.columns = [i.replace(' ', '') for i in boxoffice_cols]
df_boxoffice = df_boxoffice.with_columns(
pl.col('매출액', '누적매출액').str.replace_all(',', '').cast(pl.Int64),
pl.col('관객수', '누적관객수', '스크린수', '상영횟수').str.replace_all(',', '').cast(pl.Int64)
)
df_boxoffice = df_boxoffice.with_columns(
pl.col('장르').str.split(','),
pl.col('배우').str.split(','),
)
df_boxoffice = (df_boxoffice.filter(
pl.col('순위').is_not_null()).
filter(~(pl.col('장르').list.contains('성인물(에로)'))).
filter(pl.col('매출액') != 0).
filter(~((pl.col('관객수') <= 1) & (pl.col('누적관객수') <= 1) &
(pl.col('스크린수') <= 1) & (pl.col('상영횟수') <= 1)))
)
import altair as alt
import plotly.express as px
8장 폴라스로 영화 데이터 생생하게 그리기¶
8.1 폴라스 시각화의 특징 알아보기¶
8.2 폴라스로 손쉽게 데이터 시각화하기¶
8.3 영화 데이터를 시각적으로 풍부하게 표현하기¶
8.3.4 막대그래프로 비교한 국적별 평균 관객 수와 객단가¶
In [2]:
(df_boxoffice.filter(pl.col('순위') <= 100)
.group_by('대표국적').agg(pl.col('관객수').mean().alias('평균관객수'))
.plot.bar(x = alt.X('대표국적').sort('-y'), y = '평균관객수').properties(width = 300, height = 300))
Out[2]:
In [3]:
fig = px.bar((df_boxoffice.filter(pl.col('순위') <= 100).group_by('대표국적')
.agg((pl.col('매출액').sum() / pl.col('관객수').sum()).round(1).alias('객단가'))
.sort('객단가', descending = True)),
x = '대표국적', y = '객단가', color = '대표국적', text = '객단가',
color_discrete_map = {"한국": "red", "일본": "gray", "프랑스": "gray", "미국": "gray", "영국": "gray",
"호주": "gray", "대만": "gray", "중국": "gray"})
fig.show()
In [4]:
fig = px.bar(
(df_boxoffice.filter(pl.col('순위') <= 100, pl.col('대표국적').is_in(["한국", "미국", "일본"]))
.with_columns(
pl.when(pl.col('등급') == "15세관람가,15세이상관람가")
.then(pl.lit("15세이상관람가")).otherwise(pl.col('등급')).alias('등급'))
.group_by('등급', '대표국적').agg(pl.col('관객수').mean().round(1).alias('관객수'))),
x = '등급', y = '관객수', facet_col = '대표국적', text = '관객수',
category_orders = {"대표국적": ["한국", "미국", "일본"],
"등급": ["전체관람가", "12세이상관람가", "15세이상관람가", "청소년관람불가"]})
fig.show()